/*
 * THIS CLASS ISNT WORK PROPERLY
 * 
 * This class save the score for a specific game and save it in a file
 * and then an object can get the highscore list. 
 */
package cth.tda367.beavers.glossaryGame.model;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.*;

import cth.tda367.beavers.glossaryGame.util.IObservable;

public class HighScore implements IObservable {
	private static HighScore INSTANCE;
	private String gameName;
	private PropertyChangeSupport hiObservers;
	private String [][] buffert;
	private String fileName;
	Scanner sc = null;
	private double totalScore;
	private String names;
	ArrayList<Score> oGameScoreList = new ArrayList<Score>();
	ArrayList<Score> mGameScoreList = new ArrayList<Score>();
	ArrayList<Score> hGameScoreList = new ArrayList<Score>();
	ArrayList<Score> currentGameScoreList = new ArrayList<Score>();
	ArrayList<Score> scannerList = new ArrayList<Score>();

	
	private HighScore() {
		hiObservers =  new PropertyChangeSupport(this);
	}
	
	public static HighScore getInstance() {
		if (INSTANCE == null) {
			INSTANCE = new HighScore();
		}
		return INSTANCE;
	}
	

	public void setGameName (String gameName) {
		this.gameName = gameName;
		
	}
	
	
	public ArrayList<Score> addHighScore (String names, double totalScore, String gameName) {
		
		this.gameName = gameName;
		this.names = names;
		
		scannerList.add(new Score(names, totalScore));
		showHighScore();
		
		for(int i = 0; i < buffert[0].length; i++) {
			try{
				if(gameName.equals("oGame")){
					oGameScoreList.add(new Score(buffert[i][0],Double.parseDouble(buffert[i][1])));
				}
				else if(gameName.equals("hGame")){
					hGameScoreList.add(new Score(buffert[i][0],Double.parseDouble(buffert[i][1])));
				}
				else if(gameName.equals("mGame")){
					mGameScoreList.add(new Score(buffert[i][0],Double.parseDouble(buffert[i][1])));
				}
			}
			catch (ArrayIndexOutOfBoundsException e) {
				
			}
			
		}
		changeHighScore();
		totalScore =0;
		this.totalScore = 0;
		return currentGameScoreList;
		
		
	}
	
	
	public void currentListClear () {
		currentGameScoreList.clear();
	}
	
	
		
	public void changeHighScore(){
		
		if(gameName.equals("oGame")) {
			currentGameScoreList = oGameScoreList;
		}
		else if (gameName.equals("hGame")) {
			currentGameScoreList = hGameScoreList;
		}
		else if (gameName.equals("mGame")) {
			currentGameScoreList = mGameScoreList;
		}
		
		
	}
		
	public void scan() {
		
		try {
			sc = new Scanner(new BufferedReader(new FileReader("c:/"+fileName+".txt")));
		} catch (FileNotFoundException e1) {
		}
	}
	
	public void showHighScore(){
		
		scan();

		while(sc.hasNext()) {
			
			String name = "";
			boolean flag = true;			
			double score = 0;
			
			while(flag && sc.hasNext()) {
				String s = sc.next();
				
				if(!s.equals(":")) {
					name = name + s + " ";
				}
				else {
					flag = false;
				}
			}
			
			score = Double.parseDouble(sc.next());
			
			
			scannerList.add(new Score(name, score));
		}
		
		
		
		Collections.sort(scannerList);
		
		buffert = new String [scannerList.size()][2];
		
		for(int k = 0; k < scannerList.size(); k++){
			
				buffert[k][0] = scannerList.get(k).getName();
				buffert[k][1] = ""+scannerList.get(k).getPoint();
		}
		
		
				
		hiObservers.firePropertyChange("funkar",null,buffert);
		scannerList.clear();
		
		
	}

	@Override
	public void addObserver(PropertyChangeListener observer) {
		hiObservers.addPropertyChangeListener(observer);
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
		
	}

}
